﻿using Microsoft.Extensions.Logging;
using Simple.Framework.Configuration;
using Simple.Framework.Protocol;
using Simple.Tools;
using SimpleNet.Client;
using SimpleNet.Net;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;

namespace Test.Client
{
	internal class Program
	{
		public static int Main(string[] args)
		{
			List<Socket> sockets = new List<Socket>();
			for (int i = 0; i < 100; i++)
			{
				sockets.Add(CreateClient());
			}

			//List<ISimpleClient>simpleClients = new List<ISimpleClient>();
			//for (int i = 0; i < 200; i++)
			//{
			//	simpleClients.Add(GetClient());
			//}

			Console.ReadLine();
			return 0;
		}

		public static int TestClient()
		{
			IPEndPoint point = new IPEndPoint(ToolSimpleCheck.GetLocalHostFirstOrDefault(), 8080);
			using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
			{
				socket.Connect(point);
				bool end = true;

				while (end)
				{
					string input = Console.ReadLine();
					if ("quit"==input)
					{
						end = false;
					}
					byte[] data = GetDataPack(input, 0x18);
					socket.Send(data);

				}
			}
			return 0;
		}
		public static Socket CreateClient()
		{
			Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp);

			try
			{
				IPAddress ip;
				IPAddress.TryParse("172.16.1.105", out ip);
				IPEndPoint point = new IPEndPoint(ip, 8080);
				Console.WriteLine(ip.ToString());
				socket.ConnectAsync(point);
				Task.Run(() =>
				{
					while (true)
					{
						
						  socket.Send(GetDataPack("你好,asdfasdfasdfasdfzcdzdasdasdfcdqweqwsdfnas dasdfasdfa你好", 0x18));
						//socket.Send(Encoding.Default.GetBytes("你好,asdfasdfasdfasdfzcdzdasdasdfcdqweqwsdfnas dasdfasdfa你好"));
						Task.Delay(1000).Wait();
					};
				});
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
				Console.WriteLine(ex.StackTrace);
			}

			return socket;
		}

		public static byte[] GetDataPack(string msg,byte id)
		{
			int index = 0;
			byte[] heard = new byte[2] {0x12,0x13};

			byte[] command = new byte[1] { id };

			byte[] contant = Encoding.UTF8.GetBytes(msg);

			byte[] tail = new byte[2] {0x22,0x13};

			byte[] data=new byte[tail.Length+heard.Length+contant.Length+1];

			Array.Copy(heard,0, data, index, heard.Length);
			index += heard.Length;
			Array.Copy(command, 0, data, index, command.Length);
			index++;
			Array.Copy(contant, 0, data, index, contant.Length);
			index += contant.Length;
			Array.Copy(tail, 0, data, index, tail.Length);
			return data;
		}


		public static ISimpleClient GetClient()
		{
			 var client=SimpleBuilder.BuilderHost().Regster((container) =>
			{
				var configuration = container.GetService<IConfiguration>();
			

			}).
			ConfigutionLogger((logerProvide) =>
			{

				#region 日志模块配置
				if (ToolSimpleCheck.IsDebug())
				{
					logerProvide.CurrentLogLevel = LogLevel.Trace; //大于debug 等级日志将会被打印
				}
				else
				{
					logerProvide.CurrentLogLevel = LogLevel.Information; //大于debug 等级日志将会被打印
				}

				//logerProvide.CurrentLogLevel = LogLevel.Warning;
				logerProvide.IsConsoleWrite = true;
				logerProvide.RollingInterval = Simple.Framework.Logger.eRollingInterval.Day;//每天创建一份日志文件
				logerProvide.ProfixLogFile = "service";//日志文件名称前缀
				logerProvide.LogFolderPath = "log";//日志文件夹相对路径
				#endregion
			})
		.CreateClient();
			Task.Run(() =>
			{
				while (true)
				{
					Task.Delay(1000).Wait();
					client.SendMessage("asdfasdasdfasdfa sdfaa sdfasdfas dfasd fas你好asdfasd!",EncoderType.UTF_8);
				}
			});
			return client;
		}
	}
}